

#include "machine_stat.h"
#include "key_process.h"
#include "iodefine.h"
#include "display.h"
#include "custom_define.h"
#include "wifi_comm.h"
#include "save_date.h"



uint8_t wifi_ctrl_block_time = 0; //wifi 控制屏蔽计时（单位：50ms）
Machine_Stat MachineStat=MODE_INIT;
Machine_Stat COMM_ERR_LAST_MODE=MODE_INIT;
extern uint8_t wifi_cmd;
extern uint8_t cmd_times;
DATE_STAT date_stat;

uint8_t msg_type=0;
uint8_t dis_smartlink=0;
//uint8_t self_de=0;
//uint8_t self_in=0;
uint8_t dis_times = 0;
uint8_t key_flag=0;

uint8_t fire_err_stat=0;
uint8_t Err_Dis_Stat=0;
//用来做标记为，当它为1，表示MODE_INIT状态结束，可以在dataflash中进行取值，写值
//防止程序一开始，立即从datafataflash中取值，写值
uint8_t dis_remember=0;

//uint8_t version_test_time=0;
extern uint8_t key_num;
//extern uint8_t dis_cmd_times;

extern uint8_t wifi_cmd;

Time timeout;
Time touchtime;
Time errtime;
Time wifitime;
Time versiontime;
Time akeycycle;
Time wifi_signal;
Time one_minute_version;
Time wifi_unlink_time;
//用于计时，当时间大于等于6小时后，掉电记忆才会计时
Time six_hour;

uint8_t disakey_cycle=0;
uint8_t smartlink_start=0;
uint8_t success_wifi=0;
uint8_t wifi_send_no1=0;
//用来判断打开一键循环时，增压功能是否打开。
uint8_t super_is_ok=0;

//uint8_t akey_cycle=0;
//uint8_t wifi_send_no2=0;
uint8_t super_main_number=0x30;

HEATER_INFO heater_info;

extern uint8_t blink_flag_250ms;
extern uint8_t blink_flag_500ms;
extern uint8_t blink_flag_1000ms;
extern uint8_t blink_flag_2000ms;
extern uint8_t comm_stat;
extern uint16_t err_time_cnt;

extern uint8_t Commfail_Flag;

extern uint8_t smartlink;
extern WIFI_Stat wifi_stat;
Machine_Stat version_comm=MODE_TEST;

extern uint8_t dis_runmode;

void machine_stat(void)
{
	static uint8_t MachineStat_old=MODE_INIT;
	//判断故障是否产生变更，
	static uint8_t err_type_last=0;
	
	
	KeyProcess();
	//上电到现在是否小于6小时。小于6小时，继续计时，当等于的时候，停止计时，掉电记忆功能启动。
	if(six_hour.time_hour<15)//15
	{
		Get_Time(UnitHour,&six_hour);
		//Save_Data[0]=1;
		if(six_hour.time_hour>=15&&Save_Data[0]==0)
		{
			Save_Data[0]=1;
			Save_Data[5]=checksum(Save_Data+1,sizeof(Save_Data)-2);
			save_data_process();
		}
	}
	if((MachineStat != MODE_INIT)&&(MachineStat != MODE_ERR)){
		if(EventListener(KEY_WIFI_LONG)){
			wifi_ctrl_block_time = 0;//Aaron,如果是配网键，不再wifi按键锁定
			if (wifi_stat.net_stat !=OFFLINE)
			{			
				//display_wifi(0);
				wifi_cmd = WIFI_UNLINK;
				smartlink = 0;
				smartlink_start = 0;
				success_wifi = 0;	
				wifi_stat.net_stat = OFFLINE;
				wifi_stat.smart_link_stat = SMARTLINK_FAILED;
				wifi_send_no1 = 0;
			}else if (wifi_stat.net_stat ==OFFLINE){
				//如果WiFi没有连接上，长按WiFi键，进行配网
				wifi_stat.net_stat = OFFLINE;
				wifi_stat.smart_link_stat = SMARTLINK_FAILED;
				smartlink = 0;
				//dis_signal=0;
				//配网开始
				smartlink_start=1;
				dis_smartlink=0;
				success_wifi=0;
				wifi_cmd = WIFI_START;
				//Get_Time(TIME_CLEAN,&wifitime);
				//wifi_send_no1=1;	
				wifi_send_no1=0;
				//wifi_send_no2=0;	
			}
			
			
		}
	}
	if(smartlink_start==1)
	{		
		
		if((smartlink==1)&&(dis_smartlink==0))
		{
			//接受到09 31 smarklink为1.计时开始
			Get_Time(TIME_CLEAN,&wifitime);	
			dis_smartlink=1;
			dis_times=0;
			////WiFi信号强度开始接受。下面哪一行为WiFi强度数据接受计时
			//Get_Time(TIME_CLEAN,&wifi_signal);
		}
		//第一次配网没收到09 31，每秒5次的发送 08 31，此时 
		else if((smartlink==0)&&(dis_smartlink==0))//5 times/s
		{
			
			dis_times++;
			display_wifi(0);
			if(dis_times>=100)
			{
				dis_times = 0;
				wifi_cmd = WIFI_START;
				wifi_send_no1++;

				if(wifi_send_no1>=60)
				{
					//wifi_send_no2++;
					//wifi_send_no1=0;
				//}
				//if(wifi_send_no2==120)
				//{
					smartlink_start=0;
					wifi_send_no1=0;
					//wifi_send_no2=0;
					wifi_cmd=WIFI_INTI;
				}
			}
		}	
		//第一次配网，收到09 31，计时也打开了，开始进行闪烁和等待其他命令，
		else if((smartlink==1)&&(dis_smartlink==1))
		{
			Get_Time(UnitSec,&wifitime);
			//接受到09 31 smarklink为1.灯开始闪烁
			if(blink_flag_250ms){

				display_wifi(smartlink);
			}
			else 
			{
				display_wifi(0);
			}
			if(wifi_stat.smart_link_stat && wifi_stat.net_stat){
				//配网成功 02 31和 05 31全部收到
				display_wifi(wifi_stat.net_stat);
				//这只表示此次配网成功了。
				//success_wifi=1;
				//但是存在一种情况，那就是当连上网后，马上又断开，此时如果继续在这里运行。
				//闪烁情况便是2HZ，与断网的0.5HZ对不上，所以，需要在这里便跳下去。
				smartlink_start=0;
				//标识符为三表示连上服务器
				//dis_signal=3;
			}
			//2分钟计时到达
			if(wifitime.time_sec>300){ //2min计时到了，
				smartlink_start=0;
				Get_Time(TIME_CLEAN,&wifitime);
				//配网在其中一步失败。退回去。
				if(wifi_cmd<WIFI_ACK_3)
				{
					wifi_cmd=WIFI_INTI;
					//wifi_stat.net_stat = OFFLINE;
					//wifi_stat.smart_link_stat = SMARTLINK_FAILED;
					smartlink = 0;
				}
				dis_smartlink=0;
			}
		}
	}	
	else if(smartlink_start==0){//smartlink_start=0
		//2分钟计时到了，配网成功
		if(wifi_stat.smart_link_stat && wifi_stat.net_stat){
			//dis_signal不为3，表示服务器断开，则需要闪烁。一秒一闪d
			//if(&&blink_flag_1000ms)
			//{
			//	display_wifi(0);		
			//}
			//else
			//{
				display_wifi(1);
				//Get_Time(UnitMin,&wifi_signal);
			//}
		}//2分钟计时到了，配网不成功
		else if((wifi_stat.smart_link_stat==SMARTLINK_FAILED||wifi_stat.net_stat==OFFLINE)&&success_wifi&&blink_flag_1000ms)
		{
			display_wifi(1);
		}
		else
		{
			display_wifi(0);
			//Get_Time(TIME_CLEAN,&wifi_signal);
		}
	}
		//需要操作部对WiFi硬件复位
		//else if(smartlink_start==2)
		//{
		//	wifi_send_no1++;
		//	WIFI_RESET =0;
		//	if(wifi_send_no1>=25)
		//	{
		//		smartlink_start=0;
		//		WIFI_RESET=1;
		//		wifi_send_no1=0;
		//	}
		//}
		//如果连上服务器，进行接收电波强度计时
		//if(success_wifi)
		///{
		//	Get_Time(UnitMin,&wifi_signal);
		//}
		//else if(success_wifi==0)
		//{
		//	Get_Time(TIME_CLEAN,&wifi_signal);
		//}
		//if(wifi_signal.time_min>=50&&wifi_stat.signal!=NO_SIGNAL)//20分钟
		//{
		//	wifi_stat.signal=NO_SIGNAL;
		//	Get_Time(TIME_CLEAN,&wifi_signal);
		//}
		//else if(wifi_signal.time_min>=50&&wifi_stat.signal==NO_SIGNAL)//发送reset模块命令
		//{
		//	//cmd_times = wifi_cmd;
		//	wifi_cmd=WIFI_REset;
		//	Get_Time(TIME_CLEAN,&wifi_signal);
		//}
		
	//首先。WiFi发来电波强度的的条件是，在连接上路由器后发送，在断开路由器后便不会发送了。
	if(wifi_stat.smart_link_stat==SMARTLINK_SUCCESS)
	{
		Get_Time(UnitMin,&wifi_signal);
		if(wifi_signal.time_min>=50)//发送reset模块命令
		{
			//cmd_times = wifi_cmd;
			wifi_cmd=WIFI_REset;
			Get_Time(TIME_CLEAN,&wifi_signal);
		}
	}
    //Get_Time(TIME_CLEAN,&one_minute_version);
	if(one_minute_version.time_min<2)
	{
		Get_Time(UnitMin,&one_minute_version);
	}	
	if(EventListener(KEY_VERSION_LONG)&&version_comm==MODE_TEST&&one_minute_version.time_min<2)//第一次接受的版本查询
	{
		wifi_ctrl_block_time = 60;
		version_comm=MachineStat;
		MachineStat = MODE_VERSION;
		Get_Time(TIME_CLEAN,&versiontime);
	}
	//如果故障发生变更，立即修改
	if(err_type_last!=heater_info.err_type)
	{
		err_time_cnt=0;
		err_type_last=heater_info.err_type;
	}
	//if(one_minute_version.)
    switch(MachineStat)
    {
        case MODE_INIT: //0
			Get_Time(UnitSec,&timeout);//Get_Time(UnitSec,&timeout);//timeout++;
			display_all();//Led_On();
			if(timeout.time_sec>3){
				save_data_init();
				MachineStat=MODE_POWEROFF;
				Get_Time(TIME_CLEAN,&timeout);//timeout=0
				Get_Time(TIME_CLEAN,&wifi_signal);
				//dis_remember = 1;
				if(heater_info.SetHeatTempMax!=0x3C)
				{
					heater_info.SetHeatTempMax=0x3C;
					heater_info.SetHeatTempMin=0x23;
				}
				runing_mode[0].max_temp=heater_info.SetHeatTempMax;
				runing_mode[0].min_temp=heater_info.SetHeatTempMin;
				//第二次上电，跳转到掉电之前的模式和温度。
				if(save_rdflag)
				{
					dis_runmode = Save_Data[1];//remeber mode
					//heater_info.Send_SetHeatTemp = Save_Data[2];
					//key_flag=1;
					//key_num=3;
					runing_mode[0].init_temp=Save_Data[2];
					runing_mode[1].init_temp=Save_Data[3];
					runing_mode[2].init_temp=Save_Data[4];
					//six_hour.time_hour=15;
				}
				heater_info.running_mode = dis_runmode;
				if(1==Save_Data[0])
				{
					six_hour.time_hour=15;
				}
				//runing_mode[dis_runmode].init_temp=heater_info.Send_SetHeatTemp;
			}
		break;                            
		case MODE_POWEROFF:
		{
			display_clean();//DisplayDriverClear();
			if(EventListener(KEY_POWER_SHORT)||EventListener(KEY_POWER_LONG)){
				wifi_ctrl_block_time = 60;
				key_flag=1;key_num=1;
			}	
			if(heater_info.power_stat==0x31){
				MachineStat=MODE_RUN;
			}
			
			Err_Dis_Stat=STA_0;
			if(Commfail_Flag==1){
				COMM_ERR_LAST_MODE=MachineStat;
				MachineStat=MODE_COMM_ERR;
				comm_stat=0;
			}
		}
		break;
		case MODE_RUN:
			if(heater_info.power_stat==0x30){
				MachineStat=MODE_POWEROFF;
				break;
			}
			//关机和开机的操作
			if(EventListener(KEY_POWER_SHORT)||EventListener(KEY_POWER_LONG)){
				wifi_ctrl_block_time = 60;
				key_flag=1;
				key_num=1;
			}
			//eco的打开和关闭
			else if(EventListener(KEY_ECO_SHORT)||EventListener(KEY_ECO_LONG)){
				wifi_ctrl_block_time = 60;
				key_flag=1;
				key_num=2;
			}
			//一键循环或者增压的打开和关闭
			else if(EventListener(KEY_SUPER_SHORT))
			{
				wifi_ctrl_block_time = 60;
				//if(heater_info.akey_cycle==0x30&&heater_info.super_mode==0x30)
				//{
				//	key_flag=1;
				//	key_num=4;
				//	heater_info.send_akeycycle=0x31;
				//}
				//else if(heater_info.akey_cycle==0x31&&heater_info.super_mode==0x30)
				//{
				//	key_flag=1;
				//	key_num=6;
				//	heater_info.send_super_mode=0x31;
				//}
				//else if(heater_info.akey_cycle==0x30&&heater_info.super_mode==0x31)
				//{
				//	key_flag=1;
				//	key_num=6;
				//	heater_info.send_super_mode=0x30;
				//}
				if(heater_info.akey_cycle==0x30)
				{
					if(heater_info.super_mode==0x31)
					{
						//增压打开的时候，循环打开
						super_is_ok=1;
					}
					key_flag=1;
					key_num=4;
					heater_info.send_akeycycle=0x31;
				}
				else if(heater_info.akey_cycle==0x31)
				{
					key_flag=1;
					key_num=4;
					heater_info.send_akeycycle=0x30;
				}
				
			}
			else if(EventListener(KEY_SUPER_LONG))
			{
				wifi_ctrl_block_time = 60;
				if(heater_info.super_mode==0x30)
				{
					if(heater_info.akey_cycle==0x31)
					{
						super_is_ok=1;
					}
					else
					{
						key_flag=1;
						key_num=6;
						heater_info.send_super_mode=0x31;
					}
					
				}
				else if(heater_info.super_mode==0x31)
				{
					
					if(super_is_ok==1)
					{
						super_is_ok=0;
					}
					else
					{
						key_flag=1;
						key_num=6;
						heater_info.send_super_mode=0x30;
					}
				}
					
			}
			//下面为长按的处理机制
			//self_in=0;
			//self_de=0;
			else if(EventListener(KEY_INCREASE_LONG)){
				wifi_ctrl_block_time = 60;
				key_flag=1;
				key_num=3;
				//self_in=1;
				if(Get_Time(UnitMSec,&touchtime)>25)
				{
					Get_Time(TIME_CLEAN,&touchtime);
					if(heater_info.SetHeatTemp<48){
						heater_info.Send_SetHeatTemp++;
					}
					if(heater_info.Send_SetHeatTemp>heater_info.SetHeatTempMax){
						heater_info.Send_SetHeatTemp=heater_info.SetHeatTempMax;
					}
					
				}	
			}
			else if(EventListener(KEY_DECREASE_LONG)){
				wifi_ctrl_block_time = 60;
				key_flag=1;
				key_num=3;
				//self_de=1;
				if(Get_Time(UnitMSec,&touchtime)>25)
				{
					Get_Time(TIME_CLEAN,&touchtime);
					heater_info.Send_SetHeatTemp--;

				}
				if(heater_info.Send_SetHeatTemp<heater_info.SetHeatTempMin){
					heater_info.Send_SetHeatTemp=heater_info.SetHeatTempMin;
				}		
			}
			else
			{
				Get_Time(TIME_CLEAN,&touchtime);
			}
			//一键循环和增压的处理,此时一键循环被打开，
			if(heater_info.akey_cycle==0x31&&disakey_cycle==0)
			{
				//清理一下计数，开始计时
				Get_Time(TIME_CLEAN,&akeycycle);
				disakey_cycle=1;
				
			}
			//开始60分钟计时，
			else if(heater_info.akey_cycle==0x31&&disakey_cycle==1)
			{
				Get_Time(UnitMin,&akeycycle);
				//时间到达，关闭一键循环
				if(akeycycle.time_min==155)//结束一键循环
				{
					key_flag=1;
					key_num=4;
					heater_info.send_akeycycle=0x30;
					//disakey_cycle=0;
					Get_Time(TIME_CLEAN,&akeycycle);
				}
			}
			//关闭一键循环
			if(heater_info.akey_cycle==0x30||super_main_number==0x31)
			{
				if(heater_info.akey_cycle==0x30)
				{
					disakey_cycle=0;
				}
				Get_Time(TIME_CLEAN,&akeycycle);
			}
			if(super_is_ok==1)
			{
				//定时时间到了会关闭一键循环，或者手动关闭的一键循环
				if(heater_info.akey_cycle==0x30&&key_num!=4)
				{
					key_flag=1;
					key_num=6;
					super_is_ok=0;
					heater_info.send_super_mode=0x31;
				}
				//heater_info.super_mode=0x31;
			}
			//在各个状态下，按下增温或者减温键，数码管温度的处理
			if(dis_runmode==0x00)
			{
				if(EventListener(KEY_INCREASE_SHORT)){
					wifi_ctrl_block_time = 60;
					key_flag=1;
					key_num=3;
					heater_info.Send_SetHeatTemp++;
					if(heater_info.Send_SetHeatTemp>heater_info.SetHeatTempMax){
						heater_info.Send_SetHeatTemp=heater_info.SetHeatTempMax;
					}
				}	
				else if(EventListener(KEY_DECREASE_SHORT)){
					wifi_ctrl_block_time = 60;
					key_flag=1;
					key_num=3;
					heater_info.Send_SetHeatTemp--;
					if(heater_info.Send_SetHeatTemp<heater_info.SetHeatTempMin){
						heater_info.Send_SetHeatTemp=heater_info.SetHeatTempMin;
					}
				}
				//runing_mode[dis_runmode].init_temp = heater_info.Send_SetHeatTemp;
			}
			else if(dis_runmode==0x01||dis_runmode==0x02)
			{
				if(EventListener(KEY_INCREASE_SHORT)||EventListener(KEY_DECREASE_SHORT)){
					wifi_ctrl_block_time = 60;
					runing_mode[dis_runmode].init_temp = heater_info.SetHeatTemp ;
					dis_runmode=0x00;
					heater_info.Send_SetHeatTemp=runing_mode[dis_runmode].init_temp;
					//heater_info.SetHeatTempMax=runing_mode[dis_runmode].max_temp;
					//heater_info.SetHeatTempMin=runing_mode[dis_runmode].min_temp;
					//heater_info.running_mode=dis_runmode;
					key_flag=1;
					key_num=3;
				}
				
			}

			//下面是模式切换，或者在正常运行或者机械故障的时候数码管显示情况
			
			if(heater_info.err_type==0x02){
				MachineStat=MODE_ERR;
			}
			else if(heater_info.err_type==0x00)
			{
				display_run(heater_info.SetHeatTemp,heater_info.eco_stat,heater_info.fire_stat,heater_info.akey_cycle,heater_info.super_mode);
			}
			else if(heater_info.err_type==0x01)
			{
				//Get_Time(UnitSec,&errtime);
				switch(Err_Dis_Stat)
				{
					case STA_0:
						display_run(heater_info.SetHeatTemp,heater_info.eco_stat,heater_info.fire_stat,heater_info.akey_cycle,heater_info.super_mode);		
						//if(errtime.time_sec>4){
						if(err_time_cnt>50){
							err_time_cnt=0;
							//Get_Time(TIME_CLEAN,&errtime);
							Err_Dis_Stat=STA_1;
						}
					break;
					case STA_1:
						err_display_clean(heater_info.eco_stat,heater_info.fire_stat,heater_info.akey_cycle,heater_info.super_mode);
						//if(errtime.time_sec>0.1){
						if(err_time_cnt>2.5){
							err_time_cnt=0;
							//Get_Time(TIME_CLEAN,&errtime);
							Err_Dis_Stat=STA_2;
						}
					break;			
					case STA_2:
						display_err_action(heater_info.err_code_l_h,heater_info.err_code_l_l,heater_info.eco_stat,heater_info.fire_stat,heater_info.akey_cycle,heater_info.super_mode);
						//if(errtime.time_sec>1){
						if(err_time_cnt>10){
							err_time_cnt=0;
							//Get_Time(TIME_CLEAN,&errtime);
							Err_Dis_Stat=STA_3;
						}			
					break;
					case STA_3:
						err_display_clean(heater_info.eco_stat,heater_info.fire_stat,heater_info.akey_cycle,heater_info.super_mode);
						//if(errtime.time_sec>0.1){
							if(err_time_cnt>2.5){
								err_time_cnt=0;
							//Get_Time(TIME_CLEAN,&errtime);
							Err_Dis_Stat=STA_0;
						}
					break;
				}			
			}
			if(Commfail_Flag==1){
				COMM_ERR_LAST_MODE=MachineStat;
				MachineStat=MODE_COMM_ERR;
				comm_stat=0;
				
			}
			
		break;
			
	    case MODE_ERR:
			//Get_Time(UnitSec,&errtime);
			if(EventListener(KEY_POWER_SHORT)||EventListener(KEY_POWER_LONG)){wifi_ctrl_block_time=60;key_flag=1;key_num=1;}
			if(heater_info.power_stat==0x30){
				MachineStat=MODE_POWEROFF;
			}
			
			else if(heater_info.err_type==0||heater_info.err_type==0x01){
				MachineStat=MODE_RUN;
			}
			else if(heater_info.err_type==0x02){				
				
				switch(fire_err_stat)
				{
					case STA_FIRE_0:
						display_err_fire(heater_info.err_code_l_h,heater_info.err_code_l_l,heater_info.eco_stat,heater_info.fire_stat,heater_info.akey_cycle,heater_info.super_mode);
						//if(errtime.time_sec>1.5){
						if(err_time_cnt>15){
							err_time_cnt=0;
							//Get_Time(TIME_CLEAN,&errtime);
							fire_err_stat=STA_FIRE_1;
						}
					
					break;
					case STA_FIRE_1:
						err_display_clean(heater_info.eco_stat,heater_info.fire_stat,heater_info.akey_cycle,heater_info.super_mode);
						//if(errtime.time_sec>0.5){	
						if(err_time_cnt>5){
							err_time_cnt=0;
							//Get_Time(TIME_CLEAN,&errtime);
							fire_err_stat=STA_FIRE_0;
						}
					break;
				}
				
				//if(blink_flag_1000ms){
				//	display_err_fire(heater_info.err_code_h);
				//}
			}
			if(Commfail_Flag==1){
				COMM_ERR_LAST_MODE=MachineStat;
				MachineStat=MODE_COMM_ERR;
				comm_stat=0;
			}
		break;

		case MODE_WIFI:
			
			 
		break;
		
		case MODE_COMM_ERR:
			display_clean();
			if(Commfail_Flag==0){
				MachineStat=COMM_ERR_LAST_MODE;
			}
		break;			
		case MODE_FACTORY_TEST:

		break;

		case MODE_VERSION:
			Get_Time(UnitSec,&versiontime);
			//display_run(Rinnai_version,0x31,0x01,0x31,0x31);
			display_version(Rinnai_letter_version, Rinnai_number_version,0x31,0x01,0x31,0x31);
			if(versiontime.time_sec>10)
			{
				Get_Time(TIME_CLEAN,&versiontime);
				MachineStat=version_comm;
				version_comm=MODE_TEST;
			}
			if(EventListener(KEY_POWER_LONG)||EventListener(KEY_POWER_SHORT))
			{
				wifi_ctrl_block_time = 60;
				MachineStat=version_comm;
				version_comm=MODE_TEST;
			}
		break;
			
        default:
            break;
	}
 
	//if(MachineStat_old != MachineStat) mode_change=1;
	//else  mode_change=0; 
	//if(mode_change){
	    //Get_Time(TIME_CLEAN ,&timeout);//timeout=0;
		//Get_Time(TIME_CLEAN,&errtime);
 		//err_time_cnt=0;
	//}
 	//if(heater_info.err_type==0x00){err_time_cnt=0;}

	MachineStat_old=MachineStat;
}








